#!/usr/bin/env python

from httputil import HttpUtil
from config import logger

class VM(HttpUtil):
  def __init__(self, prefixURL=None):
    HttpUtil.__init__(self, prefixURL)

  def deployVirtualMachine(self, serviceofferingid, templateid, zoneid):
    command = "deployVirtualMachine"
    logger.debug("command=" + command)
    args = {
             "serviceofferingid": serviceofferingid,
             "templateid": templateid,
             "zoneid": zoneid
           }
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["virtualmachine"]

  def listVirtualMachines(self):
    command = 'listVirtualMachines'
    logger.debug("command=" + command)
    response = self.http_get(command)
    logger.debug(response)
    return response

  def startVirtualMachine(self, machineid):
    command = "startVirtualMachine"
    logger.debug("command=" + command)
    args = {"id": machineid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["virtualmachine"]


  def stopVirtualMachine(self, machineid):
    command = "stopVirtualMachine"
    logger.debug("command=" + command)
    args = {"id": machineid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["virtualmachine"]
  
  def rebootVirtualMachine(self, machineid):
    command = "rebootVirtualMachine"
    logger.debug("command=" + command)
    args = {"id": machineid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["virtualmachine"]

  def migrateVirtualMachine(self, machineid, hostid):
    command = "migrateVirtualMachine"
    logger.debug("command=" + command)
    args = {
             "virtualmachineid": machineid,
             "hostid": hostid
           }
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    logger.debug(response["virtualmachine"]["hostid"])
    return response["virtualmachine"]

  def destroyVirtualMachine(self, machineid):
    command = "destroyVirtualMachine"
    logger.debug("command=" + command)
    args = {"id": machineid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["virtualmachine"]

  def resetPasswordForVirtualMachine(self, machineid):
    command = "resetPasswordForVirtualMachine"
    logger.debug("command=" + command)
    args = {"id": machineid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    logger.debug(response["virtualmachine"]["password"])
    return response["virtualmachine"]
